using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System.IO;
using EventFlowLibrary.LevelComponents;


namespace ScreenManagement
{
    /// <summary>
    /// The main menu screen is the first thing displayed when the game starts up.
    /// </summary>
    public class MainMenuScreen : MenuScreen
    {
        private SpriteBatch spriteBatch;

        /// <summary>
        /// TotalGameTime.TotalMilliseconds when the screen is loaded. 
        /// We need this to subtract from so we get an accurate count when tracking time played.
        /// We cannot reset totalgametime.
        /// </summary>
        double startMilliseconds;

        #region Initialization

        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        public MainMenuScreen()
            : base("Murder for Dinner")
        {
            // Create our menu entries.
            MenuEntry newGameEntry = new MenuEntry("Begin a New Story");
            MenuEntry loadGameMenuEntry = new MenuEntry("Continue Story");
            MenuEntry optionsMenuEntry = new MenuEntry("Options");
            MenuEntry creditsMenuEntry = new MenuEntry("Credits");
            MenuEntry exitMenuEntry = new MenuEntry("Quit");

            // Hook up menu event handlers.
            newGameEntry.Selected += newGameEntrySelected;
            loadGameMenuEntry.Selected += LoadGameMenuEntrySelected;
            optionsMenuEntry.Selected += OptionsMenuEntrySelected;
            creditsMenuEntry.Selected += CreditsMenuEntrySelected;
            exitMenuEntry.Selected += OnCancel;

            // Add entries to the menu.
            MenuEntries.Add(newGameEntry);
            MenuEntries.Add(loadGameMenuEntry);
            MenuEntries.Add(optionsMenuEntry);
            MenuEntries.Add(creditsMenuEntry);
            MenuEntries.Add(exitMenuEntry);
        }

        public override void LoadContent()
        {
            base.LoadContent();

            // Load font for drawing hint
            spriteBatch = new SpriteBatch(this.ScreenManager.GraphicsDevice);
        }

        #endregion

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            startMilliseconds = gameTime.TotalGameTime.TotalMilliseconds;

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }

        #region Handle Input


        /// <summary>
        /// Event handler for when the Single Player menu entry is selected.
        /// </summary>
        void LoadGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.AddScreen(new LoadGameScreen(), e.PlayerIndex);
        }

        /// <summary>
        /// Event handler for when the options menu is selected.
        /// </summary>
        void OptionsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.AddScreen(new OptionsMenuScreen(null), e.PlayerIndex);
        }

        void CreditsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.AddScreen(new CreditsScreen(), e.PlayerIndex);
        }

        void newGameEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            // Set up a new game
            LoadVariables loadVariables = new LoadVariables();
            loadVariables.level = "Content/XML/GameLevel.xml";
            loadVariables.cameraPitch = 0.1f;
            loadVariables.cameraYaw = MathHelper.TwoPi;
            loadVariables.position = new Vector3(-175, 3.8f, 153);
            loadVariables.timePlayed = 0;
            loadVariables.gameState = new Dictionary<string, string>();
            loadVariables.gameState.Add("GameState", "Introduction");
            loadVariables.gameState.Add("BodyFound", "No");

            // Characters
            loadVariables.gameState.Add("AMarie", "Initialize");
            loadVariables.gameState.Add("CLincoln", "Introduction");
            loadVariables.gameState.Add("PLincoln", "Introduction");
            loadVariables.gameState.Add("MRussell", "Introduction");
            loadVariables.gameState.Add("JRussell", "Introduction");
            loadVariables.gameState.Add("MajorAndrews", "Initialize");
            loadVariables.gameState.Add("MJozef", "Initialize");
            loadVariables.gameState.Add("RChalcroft", "Initialize");

            // Guilt
            loadVariables.gameState.Add("LincolnGuilt", "Initialize");
            loadVariables.gameState.Add("RussellGuilt", "Initialize");

            // Support
            loadVariables.gameState.Add("AMarieCanDiscussLocket", "Cannot");
            loadVariables.gameState.Add("AMarieCanDiscussMajor", "Cannot");
            loadVariables.gameState.Add("AMarieCanDiscussPicture", "Cannot");
            loadVariables.gameState.Add("MajorAndrewsCanTalkAboutHunter", "Cannot");
            
            //LoadVariables loadVariables = new LoadVariables();
            //loadVariables.level = "Content/XML/InsideLevel.xml";
            //loadVariables.cameraPitch = 0.1f;
            //loadVariables.cameraYaw = MathHelper.TwoPi;
            //loadVariables.position = new Vector3(-80, 12.8f, 0);
            //loadVariables.timePlayed = 0;
            //loadVariables.gameState = new Dictionary<string, string>();
            //loadVariables.gameState.Add("GameState", "KeyFound");
            //loadVariables.gameState.Add("BodyFound", "Yes");

            //// Characters
            //loadVariables.gameState.Add("AMarie", "IntroductionTwo");
            //loadVariables.gameState.Add("CLincoln", "IntroductionTwo");
            //loadVariables.gameState.Add("PLincoln", "IntroductionTwo");
            //loadVariables.gameState.Add("MRussell", "IntroductionTwo");
            //loadVariables.gameState.Add("JRussell", "IntroductionTwo");
            //loadVariables.gameState.Add("MajorAndrews", "IntroductionTwo");
            //loadVariables.gameState.Add("MJozef", "IntroductionTwo");
            //loadVariables.gameState.Add("RChalcroft", "IntroductionTwo");

            //// Guilt
            //loadVariables.gameState.Add("LincolnGuilt", "Introduction");
            //loadVariables.gameState.Add("RussellGuilt", "Introduction");

            loadVariables.modelPositioning = new Dictionary<string, Dictionary<string, ModelVariables>>();

            LoadingScreen.Load(this.ScreenManager, true, e.PlayerIndex, new GameplayScreen(startMilliseconds, loadVariables));
        }


        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            MessageBoxScreen confirmExitMessageBox = new MessageBoxScreen(new string[] { "Are you sure you want to exit this game?" }, true, true, true, true);

            confirmExitMessageBox.Accepted += ConfirmExitMessageBoxAccepted;

            ScreenManager.AddScreen(confirmExitMessageBox, playerIndex);
        }


        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to exit" message box.
        /// </summary>
        void ConfirmExitMessageBoxAccepted(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.Game.Exit();
        }


        #endregion
    }
}
